package com.yazdankhah.beans;

import com.yazdankhah.application.Const;
import com.yazdankhah.utils.FileUtil;
import com.yazdankhah.utils.GenUtil;
import com.yazdankhah.utils.UnicodeUtil;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 *
 * @author ahmad
 */
public class SubTrans {

  private final String transCombinationStr;

  private List<String> subTranslationsList;

  private int subTransIndex = 0;

  private static Logger logger = Logger.getLogger(SubTrans.class);


  public SubTrans(String transCombinationStr) {
    
    this.subTranslationsList = new ArrayList<>();
    this.transCombinationStr = transCombinationStr;
    
    UnicodeUtil.showUnicodeStringInfo(transCombinationStr, logger);
  }


  public String breakTranslations() {
    
      String[] tempArr = transCombinationStr.split("[\n]");
      tempArr = GenUtil.removeBlankElements(tempArr);
      String[][] translationsTable = constructTableOfSubTranslations(tempArr);
      subTranslationsList = convertTableColumns2StrArr(translationsTable);
      
      return subTranslationsList.get(subTransIndex);
  } //method


  public String nextCombinedTranslations() {

    subTransIndex++;

    if (subTransIndex < subTranslationsList.size()) {
      return subTranslationsList.get(subTransIndex);
    } else {
      return subTranslationsList.get(--subTransIndex);
    }

  } //method


  public String previousCombinedTranslations() {

    subTransIndex--;

    if (subTransIndex >= 0) {
      return subTranslationsList.get(subTransIndex);
    } else {
      return subTranslationsList.get(++subTransIndex);
    }

  } //method
  
  
  private static String[][] constructTableOfSubTranslations(String[] translations) { 
    
    String[][] translationsTable = new String[translations.length][];

    int index = 0;

    for (String trans : translations) {

      String[] splittedTrans = trans.split(Const.MY_DELIMETER);
      translationsTable[index++] = splittedTrans;

    } //for

    return translationsTable;
  } //method


  private static List<String> convertTableColumns2StrArr(String[][] translationsTable) {

    List<String> subTranses = new ArrayList<>();

    StringBuilder sb;

    for (int j = 0; j < translationsTable[0].length; j++) {
      sb = new StringBuilder();

      for (int i = 0; i < translationsTable.length; i++) {
        sb.append((translationsTable[i][j]).trim()).append("\n\n");
      }

      subTranses.add(sb.toString());

    } //for

    return subTranses;

  } //method


  private static void myTests() {

    //Locale locale = new Locale("fa");
    String inFN = "quran\\t.txt";
    String outFN = "quran\\t2_Out.txt";

    String docStr = FileUtil.fileToString(inFN);
    UnicodeUtil.showUnicodeStringInfo(docStr, logger);
    UnicodeUtil.writeUnicodeStringInfo(docStr, outFN);

//      docStr = docStr.replaceAll("~", ". ");
//      String outFN2  = "quran\\t2_Out.txt";
//      UnicodeUtil.writeUnicodeStringInfo(docStr, outFN2);
    String[] splittedDoc = docStr.split("[~]");
    //String outFN3  = "quran\\t3_Out.txt";
    UnicodeUtil.showUnicodeStringInfo(splittedDoc[0], logger);

    //String outFN4  = "quran\\t4_Out.txt";
    UnicodeUtil.showUnicodeStringInfo(splittedDoc[1], logger);

      //System.out.println("docStr = [" + docStr + "]");
    //docStr = docStr.replaceAll("~", ". ");
  } //method

} //class
